package org.kd.ssh.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.kd.ssh.dao.GoodsDao;
import org.kd.ssh.dao.HistoryDao;
import org.kd.ssh.domain.Goods;
import org.kd.ssh.domain.History;
import org.kd.ssh.domain.UserInfo;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class GoodsService {
	private GoodsDao goodsDao;
	private HistoryDao historyDao;
	
	public void setHistoryDao(HistoryDao historyDao) {
		this.historyDao = historyDao;
	}

	public void setGoodsDao(GoodsDao goodsDao) {
		this.goodsDao = goodsDao;
	}

	public Goods findByNM(String nm) {
		return goodsDao.findByNM(nm);
	}

	public void save(Goods goods,UserInfo existUser) {
		String user = "未知";
		if(existUser != null ){
			user=existUser.getUsername();
		}
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
		if(!StringUtils.isBlank(goods.getId())){
			//id不为空，这名已存在
			Goods existGoods = goodsDao.findById(goods.getId()); //获得在数据库中已存在的商品，为持久对象
			existGoods.setAmount(goods.getAmount()+existGoods.getAmount()); //持久层对象，会自动更新到数据库中去
			
			History history = new History();
			history.setStoreTime(dateformat.format(new Date()));
			history.setAmount(goods.getAmount());
			history.setRemain(existGoods.getAmount());
			history.setType("1");
			history.setGoodses(existGoods);
			history.setUser(user);
			historyDao.save(history);
		}else{
			//不存在，新增一条数据
			goodsDao.save(goods);
			History history = new History();
			history.setStoreTime(dateformat.format(new Date()));
			history.setAmount(goods.getAmount());
			history.setRemain(goods.getAmount());
			history.setType("1");
			history.setGoodses(goods);
			history.setUser(user);
			historyDao.save(history);
		}
	}

	public List<Goods> findByNameLike(String term) {
		// TODO Auto-generated method stub
		return goodsDao.findByNameLike(term);
	}

	public Goods findByGoodsName(String name) {
		// TODO Auto-generated method stub
		return goodsDao.findByGoodsName(name);
	}

	public void out(Goods goods, UserInfo existUser) {
		if(goods.getId() == null || goods.getId().trim().length() == 0){
			// 没有id
			throw new RuntimeException("货物不存在，必须对一个存在的货物进行出库！");
		}
		// TODO Auto-generated method stub
		String user = "未知";
		if(existUser != null ){
			user=existUser.getUsername();
		}
		Goods existGoods = goodsDao.findById(goods.getId()); //获得在数据库中已存在的商品，为持久对象
		double remain = existGoods.getAmount()-goods.getAmount();
		if(remain < 0 ){
			throw new RuntimeException("商品余额为:"+existGoods.getAmount()+",数量不足");
		}
		existGoods.setAmount(remain); //持久层对象，会自动更新到数据库中去
		
		History history = new History();
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
		history.setStoreTime(dateformat.format(new Date()));
		history.setAmount(goods.getAmount());
		history.setRemain(existGoods.getAmount());
		history.setType("2");
		history.setGoodses(existGoods);
		history.setUser(user);
		historyDao.save(history);
	}

	public List<Goods> findByConditions(DetachedCriteria detachedCriteria) {
		// TODO Auto-generated method stub
		return goodsDao.findByConditions(detachedCriteria);
	}
	
	
}
